package com.ryan.demo.grpc.client.config;

import com.ryan.demo.grpc.client.provider.SubEventHubGrpcServiceImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.IOException;

/**
 * @description： RelianGrpcClientConfig
 * @Author MRyan
 * @Date 2023/10/7 21:53
 * @Version 1.0
 */
@Slf4j
@Configuration
public class RelianGrpcClientConfig {

    private Server server;

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void start() throws IOException {
        SubEventHubGrpcServiceImpl subEventHubGrpcService = applicationContext.getBean(SubEventHubGrpcServiceImpl.class);
        this.server = ServerBuilder.forPort(9090)  // 选择一个端口号
                .addService(subEventHubGrpcService)
                .build()
                .start();
        log.info("Server started, listening on " + 9090);
    }

    @PreDestroy
    public void stop() {
        if (server != null) {
            server.shutdown();
        }
    }
}
